﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Biljart.Model.Dao
{
    class TypeDao: ITypeDao
    {
        private readonly BiljartDataClassesDataContext _context;

        public TypeDao()
        {
            _context = new BiljartDataClassesDataContext();
        }

        public Type GetType(long id)
        {
            var type = (from t in _context.Types
                        where t.Id == id
                        select t).FirstOrDefault();
            return type;
        }

        public IEnumerable<Type> GetTypes()
        {
            return _context.Types;
        }

        public IEnumerable<Type> GetTypes(HashSet<long> ids)
        {
           var types = (from t in _context.Types
                        where ids.Contains(t.Id)
                        select t).ToList();
            return types;
        }

        public bool AddType(Type type)
        {
            try
            {
                _context.Types.InsertOnSubmit(type);
                _context.SubmitChanges();
                return true;
            }
            catch (SqlException)
            {
                return false;
            }
        }
    }
}
